package com.lg.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/26 15:24
 */
public class P1983NOIP2013普及组_车站分级 {

    private static final int N = 2010, M = 1000010;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M], din = new int[N];
    private static boolean[] st = new boolean[N];
    private static int[] q = new int[N], dist = new int[N];
    private static int n, m, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt(); m = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= m; i++) {
            Arrays.fill(st, false);
            int cnt = nextInt(), start = n, end = 1;
            for (int j = 0; j < cnt; j++) {
                int v = nextInt();
                start = Math.min(start, v);
                end = Math.max(end, v);
                st[v] = true;
            }
            int dummy = n + i;
            //不停靠的站点：u->dummy 的边权为0。停靠的站点：u->dummy 的边权为0，dummy->v的边权为1
            for (int j = start; j <= end; j++) {
                if (st[j]) add(dummy, j, 1);
                else add(j, dummy, 0);
            }
        }
        writer.println(resolve());
        writer.flush();
    }

    private static int resolve() {
        topsort();
        for (int i = 1; i <= n; i++) dist[i] = 1;
        //根据拓扑序列求最长路
        int ans = 0;
        for (int i = 0; i < n + m; i++) {
            int u = q[i];
            for (int j = h[u]; j != -1; j = ne[j]) {
                int v = e[j];
                dist[v] = Math.max(dist[v], dist[u] + w[j]);
                ans = Math.max(ans, dist[v]);
            }
        }
        return ans;
    }

    private static void topsort() {
        int hh = 0, tt = -1;
        for (int i = 1; i <= n + m; i++) {
            if (din[i] == 0) {
                q[++tt] = i;
                if (tt == N) tt = 0;
            }
        }
        while (hh <= tt) {
            int u = q[hh++];
            if (hh == N) hh = 0;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (--din[v] == 0) {
                    q[++tt] = v;
                    if (tt == N) tt = 0;
                }
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
        din[b]++;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
